Types are defined in xml files named with the standard file extension used by files of that type. The file contains formatting rules for the type starting with a root type element.
txt.xml
<?xml version="1.0" encoding="utf-8"?>
<type xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="types.xsd"
name="Text Document" highlightNumbers="false" escapeChar="" >
The type element supports several attributes for specifying basic information about how files of this type should be formatted. The langs element contains language transitions, states element contains state transitions, and the sets element contains sets of keywords. These collectively provide the rules the program uses to determine how to format the file.
Attribute | Description | Default Value |
name | The name of the type of file (Informational) | “UNKOWN” |
highlightNumbers | Indicates if numbers should be formatted differently (true/false) | false |
escapeChar | The character sequence used to escape special characters | “” |
octalFixes | Contains a list of prefixes and postfixes for octal numbers separated by spaces. The * is the first character for a postfix and the last character for a prefix. | “” |
binaryFixes | Contains a list of prefixes and postfixes for binary numbers separated by spaces. The * is the first character for a postfix and the last character for a prefix. | “” |
hexFixes | Contains a list of prefixes and postfixes for hexadecimal numbers separated by spaces. The * is the first character for a postfix and the last character for a prefix. | “” |
Code
Type.h
Type: Header file
Language: C++
Type.h Type.cpp
The Type class parses the xml file and stores the information contained within so it can be queried by other parts of the formatter. It also contains methods for checking for state starts and set keywords
The Type(std::string type) constructor takes the type name as an argument. It first sets all the type attributes to their default values and then tries to open the xml file for the type. It assumes the type file is located at .\types\<type>.xml relative to the location of the application. It reads the attributes on the type element and parses the lang, state, and set sub-elements by passing them to a constructor specified in their respective classes and adding them to the appropriate vectors.
The type class has several functions which use the attributes of the type to find and possibly print values based on the current line.
the FindBaseType() method loops through the language transitions defined on this type and tries to find one that's marked as being a base type transition. If a base transition is found it gets returned, otherwise NULL is returned.
the FindStarState(std::string line, int pos, TypeIdPair typeId) method loops through the states defined on the type to see if any of them start at the specified position in the specified line and when in the specified type name and state id combination. If a state is found it is returned by the method, otherwise NULL is returned. See State Transition for more details.
The FindAndPrintSetWord(std::stringstream& lineStream, std::string line, int pos, TypeIdPair typeId, char preC, State currentState) method loops through the sets defined on the type to see if any of them contains keywords at the specified position in the specified line and when in the specified type name and state id combination. This is done by calling the FindAndPrintSetWord() method of each set in turn. If one of the sets successfully prints a keyword then the method will return a non-zero. This is the length of the word printed. The search stops at that point and the length is returned. If no words are printed then 0 will be returned to indicate no keywords were printed. See Keyword Set for more details.
the FindAndPrintNumber(std::stringstream& lineStream, std::string line, int pos) method tries to find and print a number at the specified pos in line. It first checks to see if number highlighting is enabled for this type and then calls the TryGetNumber() method to try and get a number from the line. If the method returns a non-empty string then the number gets printed along with the start and end span and the number length is returned.
the TryGetNumber(std::string line, int pos) method tries to find a number at the specified pos in line. It first checks for prefixed binary, octal and hexadecimal numbers. It then checks for postfixed binary and octal numbers, decimal numbers without a prefix or a postfix and then postfixed hexadecimal numbers. If any of these checks finds number, indicated by the functions returning a non-empty string, then it gets returned immediately, otherwise the search continues and if no numbers are found an empty string gets returned.
the TryGetPrefixNumber(std::string line, int pos, std::vector<std::string> prefixes, std::regex numberRegEx) method tries to find a prefixed number. It first loops through the list of prefixes to see if any of them match the current line. If a prefix matches it then tries to get a number from that line matching the passed in regex. If a number is found then the prefix and number will get returned if the next char is valid, otherwise an empty string gets returned.
the TryGetPostfixNumber(std::string line, int pos, std::vector<std::string> postFixes, std::regex numberRegEx) method tries to find a postfixed number. It first tries to get a number from the line matching the passed in regex. If a number is found it then loops through the list of postfixes to see if any of them match the characters after the number. If a number is found then the prefix and number will get returned if the next char is valid, otherwise an empty string gets returned.
the GetNumberFromLineIfNextCharValid(std::string line, int pos, int length) method checks the character in line at pos + length + 1 to see if it's an alphanumeric character. If it is then an empty string is returned, otherwise length characters from line starting at pos are returned. This is done to ensure that only isolated numbers are returned.